package com.easy.order.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.easy.bean.Address;
import com.easy.bean.Orders;
import com.easy.bean.OrdersDetails;
import com.easy.bean.Products;
import com.easy.bean.ShoppingAddress;
import com.easy.bean.Users;
import com.easy.config.SYSConfig;
import com.easy.order.mapper.IOrderDetailMapper;
import com.easy.order.mapper.IOrderMapper;
import com.easy.order.mapper.ShoppingAddressMapper;
import com.easy.order.service.CartFeignService;
import com.easy.order.service.GoodsFeignService;
import com.easy.order.service.IOrderService;
import com.easy.order.service.PaymentFiegnService;
import com.easy.order.service.RedisFeignService;
import com.easy.order.service.UserFeignService;
import com.easy.util.EasyUtil;

@Service
public class OrderServiceImpl implements IOrderService {
    @Autowired
    GoodsFeignService goodsFS;
    @Autowired
    IOrderMapper orderM;
    @Autowired
    IOrderDetailMapper detailM;
    @Autowired
    UserFeignService userFS;
    @Autowired
    CartFeignService cartFS;
    @Autowired
    ShoppingAddressMapper saddressM;
    @Autowired
    PaymentFiegnService paymentFS;
    @Autowired
    RedisFeignService redisS;
    
    @Override
    public List<Orders> getOrderListByUser(Users user) {
    	QueryWrapper<Orders> qw=new QueryWrapper<>();
    	qw.eq("userid", user.getId());
    	qw.orderByAsc("createdate");
    	List<Orders> list=orderM.selectList(qw);
    	return list;
    }
    
    
    @Override
    public int changeOrderStatus(int orderid, int status) {
    	UpdateWrapper<Orders> uw=new UpdateWrapper<>();
    	uw.eq("id", orderid);
    	uw.set("status", status);
    	return orderM.update(uw);
    }
    public Orders getOrderInfo(int id) {
    	Orders order=orderM.selectById(id);
    	QueryWrapper qw=new QueryWrapper<>();
    	qw.eq("orderid", id);
    	List<OrdersDetails> list=detailM.selectList(qw);
    	order.setDetails(list);
    	return order;
    }

    @Override
    public int insertOrder(Orders order) {
        //订单总额
        BigDecimal total=new BigDecimal(0);
        for (OrdersDetails item : order.getDetails()) {
            //前台传递的商品id
            int productid = item.getProductsid();
            //通过商品id查询商品数据
            Products pro= goodsFS.getProducts(productid);
            //设置订单明细对象中的商品价格和名称
            item.setProductsname(pro.getName());
            item.setPrice(pro.getPrice());
            total=total.add(pro.getPrice().multiply(new BigDecimal(item.getQuantity())));
        }
        //订单编号
        String ordercode= UUID.randomUUID().toString();
        order.setOrdernumber(ordercode);
        order.setTotalprice(total);
        order.setStatus(1);
        //order中的userid已经在contoller中设置完成
        //1.在订单表中插入数据
        orderM.insert(order);
        int orderid=order.getId();

        for(OrdersDetails item:order.getDetails()){
            item.setOrderid(orderid);
        }
        //2.插入订单明细
        detailM.insert(order.getDetails());


        //3.插入收货地址
        //获取收货地址的数据  封装成shoppingaddress对象
        Address addr=userFS.getAddress(order.getAddr().getId());
        ShoppingAddress saddr=new ShoppingAddress();
        EasyUtil.shoppingObj(addr, saddr);
        saddr.setOrderid(orderid);
        saddressM.insert(saddr);
        //4.删除购物车中对应的商品数据
        for (OrdersDetails item: order.getDetails()){
            cartFS.deleteCart(order.getUserid(),item.getProductsid());
        }
        String codeurl= paymentFS.prePayment(order);
        String rediskey= SYSConfig.ORDER_PAYMENT_PRE_REDIS_KEY+order.getId();
        redisS.setVal(rediskey,codeurl);
        return order.getId();
    }

}
